package com.samphanie.mbg.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.samphanie.common.constants.SystemConstants;
import com.samphanie.common.filter.SQLFilter;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 分页工具类
 *
 * @author ZSY
 * @email 1451691457@qq.com
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageUtils implements Serializable {
    private static final long serialVersionUID = -1683800405530086022L;

    public static void startPage(Map<String, Object> params) {
        //分页参数
        int current = 1;
        int pageSize = 10;

        if (params.get(SystemConstants.PAGE) != null) {
            current = Integer.parseInt((String) params.get(SystemConstants.PAGE));
        }
        if (params.get(SystemConstants.LIMIT) != null) {
            pageSize = Integer.parseInt((String) params.get(SystemConstants.LIMIT));
        }
        String order = (String) params.get("order");
        String orderByColumn = (String) params.get("column");
        if (StrUtil.isNotBlank(orderByColumn)) {
            String orderBy = SQLFilter.sqlInject(orderByColumn);
            if (StrUtil.isNotBlank(order)) {
                if (SystemConstants.ASC.equalsIgnoreCase(order) || SystemConstants.DESC.equalsIgnoreCase(order)) {
                    orderBy = orderBy + " " + order;
                }
            }
            // 开启分页
            PageHelper.startPage(current, pageSize, orderBy);
        } else {
            // 开启分页
            PageHelper.startPage(current, pageSize);
        }
    }
    public static void startPage(Integer current, Integer pageSize) {
        // 开启分页
        startPage(current, pageSize, null, null);
    }
    public static void startPage(Integer current, Integer pageSize, String order, String orderByColumn) {

        current = current != null ? current : 1;
        pageSize = pageSize != null ? pageSize : 10;

        //排序字段
        //防止SQL注入（因为排序字段是通过拼接SQL实现排序的，会有SQL注入风险）
        String orderBy = SQLFilter.sqlInject(orderByColumn);
        if (StrUtil.isNotBlank(order)) {
            if (SystemConstants.ASC.equalsIgnoreCase(order) || SystemConstants.DESC.equalsIgnoreCase(order)) {
                orderBy = orderBy + " " + order;
            }
        }
        PageHelper.startPage(current, pageSize, orderBy);
    }


    /**
     * 总记录数
     */
    private int total;
    /**
     * 每页记录数
     */
    private int pageSize;
    /**
     * 总页数
     */
    private int totalPage;
    /**
     * 当前页数
     */
    private int current;
    /**
     * 列表数据
     */
    private List<?> list;
    /**
     * 是否有上一页
     */
    private boolean hasPreviousPage;
    /**
     * 是否有下一页
     */
    private boolean hasNextPage;

    /**
     * 分页
     *
     * @param list       列表数据
     * @param total 总记录数
     * @param pageSize   每页记录数
     * @param current   当前页数
     */
    public PageUtils(List<?> list, int total, int pageSize, int current) {
        this.list = list;
        this.total = total;
        this.pageSize = pageSize;
        this.current = current;
        this.totalPage = (int) Math.ceil((double) total / pageSize);
    }

    public PageUtils(List<?> list, Integer current, Integer pageSize) {

        if (CollUtil.isNotEmpty(list)) {
            this.total = list.size();
            this.pageSize = pageSize;
            this.current = current;
            this.totalPage = (int) Math.ceil((double) total / pageSize);

            if (this.current < 1) {
                this.current = 1;
            }
            int size = list.size();
            int pageCount = size / pageSize;
            int fromIndex = (this.current - 1) * pageSize;
            int toIndex = fromIndex + pageSize;
            if (toIndex >= size) {
                toIndex = size;
            }
            if (this.current > pageCount + 1) {
                fromIndex = 0;
                toIndex = 0;
            }
            this.list = list.subList(fromIndex, toIndex);
        } else {
            this.total = 0;
            this.pageSize = 0;
            this.current = 0;
            this.totalPage = 0;
            this.list = new ArrayList<>();
        }
    }

    /**
     * Mybatis-Plus 自带分页
     */
    public PageUtils(IPage<?> page) {
        this.list = page.getRecords();
        this.total = Math.toIntExact(page.getTotal());
        this.pageSize = Math.toIntExact(page.getSize());
        this.current = Math.toIntExact(page.getCurrent());
        this.totalPage = Math.toIntExact(page.getPages());
        this.hasPreviousPage = this.current > 1;
        this.hasNextPage = this.current < this.totalPage;
    }
    /**
     * pageHelper分页
     */
    public PageUtils(List<?> list) {
        PageInfo<?> pageInfo = new PageInfo<>(list);
        this.list = pageInfo.getList();
        this.total = Math.toIntExact(pageInfo.getTotal());
        this.pageSize = pageInfo.getPageSize();
        this.current = pageInfo.getPageNum();
        this.totalPage = pageInfo.getPages();
        this.hasPreviousPage = pageInfo.isHasPreviousPage();
        this.hasNextPage = pageInfo.isHasNextPage();
    }

}
